package com.woniuxy.consumer;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import com.woniuxy.entity.InfoLog;
import com.woniuxy.entity.SickBed;
import com.woniuxy.entity.WardManage;
import com.woniuxy.entity.dto.WardDTO;
import com.woniuxy.mapper.SickBedMapper;
import com.woniuxy.mapper.TdeMapper;
import com.woniuxy.mapper.WardManageMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author：xurice
 * @date：Created in 2024/4/19 16:53
 * @description：添加用药明细消费者
 * @modified By：
 * @version: $
 */
@Component
public class AddWardConsumer {
    @Resource
    private WardManageMapper wardManageMapper;

    @Resource
    private SickBedMapper sickBedMapper;
    @Transactional(rollbackFor = Exception.class)
    @RabbitListener(queues = "addWardQueue")
    public void receive(String msg, Channel channel, Message message){
        try {
            WardManage wardManage = JSON.parseObject(msg, WardManage.class);
            wardManageMapper.addWard(wardManage);
            List<SickBed> bedArrayList = new ArrayList<>();
            for (int i = 1; i <= wardManage.getSrTotalNum(); i++) {
                SickBed sickBed = new SickBed();
                sickBed.setBNo(wardManage.getId()+"A"+i+"号床");
                sickBed.setBRoomId(wardManage.getId());
                bedArrayList.add(sickBed);
            }

            sickBedMapper.addSickBed(bedArrayList);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}
